package com.hywood.manager.live.dao;

import com.hywood.manager.live.model.Channel;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface LiveDao {
    /**
     * 拉取列表，请使用分页插件查询
     *
     * @return list
     */
    @Select("SELECT * FROM l_channel WHERE `status`=0 ")
    List<Channel> listChannel();

    /**
     * 根据guid获取单个频道信息
     *
     * @param guid 频道guid
     * @return 频道信息
     */
    @Select("SELECT * FROM l_channel WHERE live_guid = #{guid}")
    Channel getChannelByGuid(String guid);

    /**
     * 根据guid获取单个频道信息
     *
     * @param channelId 频道guid
     * @return 频道信息
     */
    @Select("SELECT 1 FROM l_channel WHERE channel_id = #{channelId} LIMIT 1")
    Integer getChannelByChannelId(String channelId);

    /**
     * 插入数据
     *
     * @param channel 要保存的数据
     */
    @Insert("INSERT INTO l_channel (live_guid , channel_name,activity_name,brand_logo_path,cover_path," +
            "live_startdate,live_enddate,activity_linkurl,channel_id,channel_password,create_user,model1_name," +
            "model1_height,model1_weight,model2_name,model2_height,model2_weight,remark,update_user)" +
            "VALUES (#{liveGuid},#{channelName},#{activityName},#{brandLogoPath}," +
            "#{coverPath},#{liveStartdate},#{liveEnddate},#{activityLinkurl},#{channelId},#{channelPassword},#{createUser}," +
            "#{model1Name},#{model1Height},#{model1Weight},#{model2Name},#{model2Height},#{model2Weight},#{remark},#{updateUser})")
    void insert(Channel channel);

    /**
     * 逻辑删除数据
     *
     * @param guid guid
     */
    @Update("UPDATE l_channel SET `status`=1 WHERE live_guid = #{guid}")
    void deleteByGuid(String guid);

    /**
     * 更新频道所有信息
     *
     * @param channel 频道
     */
    @Update("UPDATE l_channel SET channel_name=#{channelName}, activity_name=#{activityName}, brand_logo_path=#{brandLogoPath}," +
            " cover_path=#{coverPath}, live_startdate=#{liveStartdate}, live_enddate=#{liveEnddate}, activity_linkurl=#{activityLinkurl}," +
            "channel_password=#{channelPassword}, create_user=#{createUser}, model1_name=#{model1Name}, " +
            "model1_height=#{model1Height}, model1_weight=#{model1Weight},model2_name=#{model2Name},model2_height=#{model2Height}," +
            " model2_weight=#{model2Weight}, remark=#{remark}, update_user=#{updateUser} WHERE live_guid = #{liveGuid}")
    void updateAllByGuid(Channel channel);

    /**
     * 更新模特信息
     *
     * @param channel 频道
     */
    @Update("UPDATE l_channel SET  model1_name=#{model1Name},model1_height=#{model1Height}, model1_weight=#{model1Weight}," +
            "model2_name=#{model2Name},model2_height=#{model2Height}, model2_weight=#{model2Weight}, remark=#{remark}," +
            "update_time= now() WHERE live_guid = #{liveGuid}")
    void updateModel(Channel channel);

    /**
     * 关闭推流
     *
     * @param guid guid
     */
    @Update("UPDATE l_channel SET `live_status`=5 WHERE live_guid = #{guid}")
    void closeByGuid(String guid);
    /**
     * 直播上下线
     *
     * @param guid guid
     * @param onlineStatus 上下线状态
     */
    @Update("UPDATE l_channel SET `online_status`=#{onlineStatus} WHERE live_guid = #{guid}")
    void setChannelOnlineStatus(@Param("guid")String guid,@Param("onlineStatus") Integer onlineStatus);

    /**
     * 条件查询
     *
     * @param channel
     * @return
     */
    @Select("<script>SELECT * FROM l_channel WHERE `status`=0 " +
            "  <if test='liveStartdate!=null and liveEnddate != null '>AND (live_startdate &gt; #{liveStartdate} AND live_enddate &lt; #{liveEnddate})</if>  \n" +
            "  <if test='channelName!=null'>AND channel_name like #{channelName} </if>  \n" +
            "  <if test='activityName!=null'>AND activity_name like #{activityName} </if>  \n" +
            "  <if test='onlineStatus!=null'>AND online_status = #{onlineStatus} </if>  \n" +
            "  <if test='liveStatus!=null'> \n" +
            "    <choose> \n" +
            "      <when test='liveStatus==5'>AND live_status = 5</when>  \n" +
            "      <otherwise> \n" +
            "        <choose> \n" +
            "          <when test='liveStatus==4'>AND (live_status = 4 or live_enddate &lt; now()) AND live_status != 5 </when>  \n" +
            "          <otherwise> \n" +
            "            <choose> \n" +
            "              <when test='liveStatus==9'>AND (live_status = 0 AND live_startdate &lt; now() AND live_enddate &gt; now())</when>  \n" +
            "              <otherwise> \n" +
            "                <choose> \n" +
            "                  <when test='liveStatus==0'>AND (live_status = 0 AND live_startdate &gt; now())</when>  \n" +
            "                  <otherwise>AND (live_status = #{liveStatus} AND live_enddate &gt; now())</otherwise> \n" +
            "                </choose> \n" +
            "              </otherwise> \n" +
            "            </choose> \n" +
            "          </otherwise> \n" +
            "        </choose> \n" +
            "      </otherwise> \n" +
            "    </choose> \n" +
            "  </if> \n" +
            "</script>\n")
    List<Channel> queryChannel(Channel channel);
}
